<?php

namespace app\aiv110\controller;

use app\aiv110\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\aiv110\model\ItemClass;
use app\aiv110\model\Item;
use app\aiv110\model\Custom;
use app\aiv110\model\CtmSpecial;
use app\aiv110\model\Opinion;

class IndexController extends CommonController {
    /*
     * code列表
     */

    public function code() {
        /*
         * 0  成功
         * 1  参数错误
         * 2  验证失败
         * 70000  您已经填过了
         * 70001  失败
         * 70002  
         * 70003
         * 70004
         */
    }

    /*
     * 首页（项目列表）
     * author xfwed 
     * date 2019.11.19
     * 
     */

    public function index() {
        $rule = [
            'classId' => 'require',
        ];
        $msg = [
            'classId.require' => '一级标签必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }
        $classId = $all['classId'];
        if ($classId == 0) {
            $classList = ItemClass::where('level', 2)->select();
        } else {
            $classList = ItemClass::where('pid', $classId)->select();
        }
        $list = [];
        foreach ($classList as $k => $v) {
            $itemList = Item::where('item_type', $v['id'])
                    ->where('is_del',1)
                    ->field('id,item_img itemImg,item_name itemName,item_price itemPrice,sell_count sellCount,special_money specialMoney')
                    ->select();
            if ($itemList) {
                $list[$k]['itemList'] = $itemList;
                $list[$k]['className'] = $v['name'];
            }
        }
        $list = array_values($list);

        return Utils::ajaxReturn(0, '查询成功', $list);
    }
    /*
     * 查询用户的级别
     * author xfwed 
     * date 2020.02.02
     * 
     */

    public function getCtmStatus() {
        $rule = [
            'ctm_id' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '用户id必须',
        ];

        $all = input('param.');
        $version = input('param.version');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }
        $ctm_id = $all['ctm_id'];
        $ctmRow = Custom::where('id',$ctm_id)->field('vip_status')->find();
        if($ctmRow['vip_status']==2){
            $row = CtmSpecial::where('ctm_id',$ctm_id)
                    ->where('type',3)
                    ->order('id desc')
                    ->limit(0,1)
                    ->select();
            if($row){
                $ctmRow['vip_status'] = 4;
            }
                    
        }
        
        if(empty($version)){
            $isshow = 1;
        }  else {
            $row = Db::table('ai_xcx_audit')->where('version',$version)
                ->where('is_open',1)
                ->find();
            if($row){
                $isshow = 2;
            }  else {
                $isshow = 1;
            }
        }
        $ctmRow['isshow'] = $isshow;
        return Utils::ajaxReturn(0, '查询成功', $ctmRow);
    }
    
    
    /*
     * 用户吐槽
     * 作者 xfwed
     * 日期 2020-06-29
     */
    public function addOpinion(){
        $rule = [
            'type' => 'require',
            'userid' => 'require',
            'content' => 'require',

        ];
        $msg = [
            'type.require' => '类型必须',
            'userid.require' => '用户id必须',
            'content.require' => '内容必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }
        $type = $all['type'];
        $userid = $all['userid'];
        $content = $all['content'];
        $data['type'] = $type;
        $data['userid'] = $userid;
        $data['content'] = $content;
        $data['add_time'] = date('Y-m-d H:i:s');
        
        $row = Opinion::insert($data);
        if($row){
            return Utils::ajaxReturn(0, '吐槽成功', null);
        }  else {
            return Utils::ajaxReturn(10000, '吐槽失败', null);
        }
    }
    
    /*
     * 首页（直播入口）
     * author xfwed 
     * date 2021.03.05
     * 
     */

    public function zhibo() {
        $data = Db::table('sys_dictionary')
                ->where('type','ZHIBO_IS_SHOW')
                ->field('code,sort goodid')
                ->find();
        $data['type'] = 2;//1商品 2项目
        return Utils::ajaxReturn(0, '查询成功', $data);
    }
    
    /*
     * 用户预约调研
     * author xfwed 
     * date 2021.07.19
     * 
     */

    public function yuyueSurvey() {
        $rule = [
            'ctm_id' => 'require',
            'is_will' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '用户id必须',
            'is_will.require' => '是否愿意必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }
        $ctm_id = $all['ctm_id'];
        $is_will = $all['is_will'];
        $why = input('param.why');
        $row = Db::name('yuyue_survey')
                ->where('ctm_id',$ctm_id)
                ->find();
        if($row){
            return Utils::ajaxReturn(70000, '您已经填过了', NULL);
        }
        $data['ctm_id'] = $ctm_id;
        $data['is_will'] = $is_will;
        $data['why'] = $why;
        $data['add_time'] = date('Y-m-d H:i:s');
        
        $row = Db::name('yuyue_survey')->insert($data);
        if($row){
            return Utils::ajaxReturn(0, '成功', null);
        }  else {
            return Utils::ajaxReturn(70001, '失败', null);
        }
    }
    
    /*
     * 魔镜AI测肤报告
     * author xfwed 
     * date 2021.08.19
     * 
     */

    public function mojingSkin() {
        $rule = [
            'ctm_id' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '用户id必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }
        $ctm_id = $all['ctm_id'];
        $cs_time = input('param.cs_time');//选择的时间
        if (empty($cs_time)) {
            $cs_time = Db::name('het_data')
                    ->where('ctm_id', $ctm_id)
                    ->where('type',1)
                    ->order('add_time desc')
                    ->value('add_time');
        }
        $c = strtotime($cs_time);
        $start_time = date('Y-m-d',$c).' 00:00:00';
        $end_time = date('Y-m-d',$c).' 23:59:59';

        //累计测肤次数
        $all_skin = Db::name('het_data')
                ->where('ctm_id',$ctm_id)
                ->where('type',1)
                ->count();
        //测肤图标
        $subQuery = Db::table('ai_het_data')
                ->where('ctm_id',$ctm_id)
                ->where('type',1)
                ->order('add_time asc')
                ->group("date_format(add_time,'%Y-%m-%d')")
                ->column("max(id)");
        $list = Db::table('ai_het_data')    
                ->where('id','in',$subQuery)
                ->field('id,ctm_id,h5url,score,add_time')
                ->order('add_time asc')
                ->select();
       //某一天的测肤明细
        $daylist = Db::table('ai_het_data')    
                ->where('ctm_id',$ctm_id)
                ->where('type',1)
                ->where('add_time','between',[$start_time,$end_time])
                ->field('id,ctm_id,h5url,score,add_time')
                ->order('add_time asc')
                ->select();
        //某一天的服务项目
        $yuyueList = Db::name('yuyue')
                ->alias('y')
                ->join('item i','i.id = y.item_id')
                ->where('y.staff_time','between',[$start_time,$end_time])
                ->where('y.ctm_id',$ctm_id)
                ->field('i.id,i.item_name,i.vice_name,i.item_price,i.special_money,i.item_img')
                ->select();
        
        $data['$all_skin'] = $all_skin;
        $data['list'] = $list;
        $data['daylist'] = $daylist;
        $data['yuyueList'] = $yuyueList;
        return Utils::ajaxReturn(0, '成功', $data);
    }
    
    /*
     * 首页（是否弹中秋弹框）
     * author xfwed 
     * date 2021.09.17
     * 
     */

    public function midAutumn() {
        //判断活动时间9月18日-9月25日 判断是否领取过  
        $ctm_id = input('param.ctm_id');
        $start_time = 1631721600;//1631894400
        $end_time = 1633622400;
        $time = time();
        if($time >= $start_time && $time <= $end_time){
            $row = Db::name('ctm_cun')
                    ->where('ctm_id',$ctm_id)
                    ->where('cun_id',146)
                    ->find();
            if($row){
              $tan = 2;  
            }  else {
                $tan = 1;  
            }
        }  else {
            $tan = 2;
        }
        $data['tan'] = $tan;
        return Utils::ajaxReturn(0, '查询成功', $data);
    }
    
     /*
     * 首页（领取中秋节代金券）
     * author xfwed 
     * date 2021.09.17
     * 
     */

    public function getMidAutumnCun() {
        
    $rule = [
            'ctm_id' => 'require',
        ];
        $msg = [
            'ctm_id.require' => '用户id必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }
        $ctm_id = $all['ctm_id'];
        $cun_id = [146,147];
        $row = Db::name('ctm_cun')
                    ->where('ctm_id',$ctm_id)
                    ->where('cun_id',146)
                    ->find();
        if($row){
            return Utils::ajaxReturn(3, '您已经领过了', null);
        }
        //给用户领劵
        for($i =0 ;$i <count($cun_id);$i++){
            //代金券数据
            $couponRow = Db::name('coupon')
                    ->where('id',$cun_id[$i])
                    ->field('id,start_time,mature')
                    ->find();
            $data['type'] = 1;
            $data['ctm_id'] = $ctm_id;
            $data['cun_id'] = $cun_id[$i];
            $data['add_time'] = date('Y-m-d H:i:s');
            $data['start_time'] = $couponRow['start_time'];
            $data['end_time'] = $couponRow['mature'];
            Db::name('ctm_cun')->insert($data);
            Db::name('coupon')->where('id',$cun_id[$i])->setInc('send_number');
        }
        return Utils::ajaxReturn(0, '领取成功', null);
}



}
